class LocalStore {
  constructor(type = 'session') {
    this._store = window[type + 'Storage']
  }

  get(key, defaultValue = '') {
    const value = this._store.getItem(key)
    if (!value) {
      return defaultValue
    }
    return JSON.parse(value)
  }

  set(key, value) {
    this._store.setItem(key, JSON.stringify(value))
    return this
  }

  remove(key) {
    this._store.removeItem(key)
    return this
  }

  has(key) {
    const value = this._store.getItem(key)
    return !!value
  }
}

export default (type = 'session') => new LocalStore(type)
